/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * DatabaseConfig.java
 *
 * Created on Aug 27, 2011, 5:05:37 PM
 */
package cw.ui;

import cw.entity.Database;
import cw.server.DatabaseManagement;
import cw.ulti.Validator;
import java.awt.Color;

/**
 *
 * @author VinhTC
 */
public class DatabaseConfig extends javax.swing.JPanel {

    Validator val = new Validator();

    /** Creates new form DatabaseConfig */
    public DatabaseConfig() {
        initComponents();
    }

    /**
     * resetAllField
     */
    public void resetAllField() {
        jlblStatus.setText("");
        jtxtServerName.setText("");
        jtxtDatabaseName.setText("");
        jtxtUserName.setText("");
        jpsfPassword.setText("");
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jtxtServerName = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jtxtUserName = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jtxtDatabaseName = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jbtnReset = new javax.swing.JButton();
        jbtnSaveChanged = new javax.swing.JButton();
        jbtnHintConfig = new javax.swing.JButton();
        jpsfPassword = new javax.swing.JPasswordField();
        jLabel10 = new javax.swing.JLabel();
        jlblStatus = new javax.swing.JLabel();

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/PuzzleLogo.png"))); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Configuration database"));
        jPanel1.setName("jPanel1"); // NOI18N

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel2.setText("Server name: ");
        jLabel2.setName("jLabel2"); // NOI18N

        jtxtServerName.setName("jtxtServerName"); // NOI18N

        jLabel3.setForeground(new java.awt.Color(204, 0, 0));
        jLabel3.setText("(*)");
        jLabel3.setName("jLabel3"); // NOI18N

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel4.setText("User name:");
        jLabel4.setName("jLabel4"); // NOI18N

        jtxtUserName.setName("jtxtUserName"); // NOI18N

        jLabel5.setForeground(new java.awt.Color(204, 0, 0));
        jLabel5.setText("(*)");
        jLabel5.setName("jLabel5"); // NOI18N

        jLabel6.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel6.setText("Password:");
        jLabel6.setName("jLabel6"); // NOI18N

        jLabel7.setForeground(new java.awt.Color(204, 0, 0));
        jLabel7.setText("(*)");
        jLabel7.setName("jLabel7"); // NOI18N

        jtxtDatabaseName.setName("jtxtDatabaseName"); // NOI18N

        jLabel8.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel8.setText("Database name:");
        jLabel8.setName("jLabel8"); // NOI18N

        jLabel9.setForeground(new java.awt.Color(204, 0, 0));
        jLabel9.setText("(*)");
        jLabel9.setName("jLabel9"); // NOI18N

        jbtnReset.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/cancel.png"))); // NOI18N
        jbtnReset.setText("Reset");
        jbtnReset.setName("jbtnReset"); // NOI18N
        jbtnReset.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnResetActionPerformed(evt);
            }
        });

        jbtnSaveChanged.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/45.png"))); // NOI18N
        jbtnSaveChanged.setText("Save changed");
        jbtnSaveChanged.setName("jbtnSaveChanged"); // NOI18N
        jbtnSaveChanged.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnSaveChangedActionPerformed(evt);
            }
        });

        jbtnHintConfig.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/export16.png"))); // NOI18N
        jbtnHintConfig.setText("Hint config");
        jbtnHintConfig.setName("jbtnHintConfig"); // NOI18N
        jbtnHintConfig.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnHintConfigActionPerformed(evt);
            }
        });

        jpsfPassword.setName("jpsfPassword"); // NOI18N

        jLabel10.setForeground(new java.awt.Color(204, 204, 204));
        jLabel10.setText("(*) is requirement");
        jLabel10.setName("jLabel10"); // NOI18N

        jlblStatus.setName("jlblStatus"); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jlblStatus, javax.swing.GroupLayout.PREFERRED_SIZE, 389, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel2)
                                .addComponent(jLabel4)
                                .addComponent(jLabel6)
                                .addComponent(jLabel8))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(jPanel1Layout.createSequentialGroup()
                                    .addComponent(jtxtDatabaseName, javax.swing.GroupLayout.PREFERRED_SIZE, 226, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(jLabel9))
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addComponent(jpsfPassword, javax.swing.GroupLayout.DEFAULT_SIZE, 226, Short.MAX_VALUE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jLabel7))
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addComponent(jtxtUserName, javax.swing.GroupLayout.PREFERRED_SIZE, 226, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jLabel5))
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addComponent(jtxtServerName, javax.swing.GroupLayout.PREFERRED_SIZE, 226, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jLabel3))))
                            .addGap(187, 187, 187))
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                            .addComponent(jbtnHintConfig)
                            .addGap(18, 18, 18)
                            .addComponent(jbtnReset, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(18, 18, 18)
                            .addComponent(jbtnSaveChanged)
                            .addGap(114, 114, 114))
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addComponent(jLabel10, javax.swing.GroupLayout.PREFERRED_SIZE, 213, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addContainerGap(314, Short.MAX_VALUE)))))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jtxtServerName, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3)
                    .addComponent(jLabel2))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jtxtUserName, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5)
                    .addComponent(jLabel4))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(jLabel6)
                    .addComponent(jpsfPassword, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jtxtDatabaseName, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel9)
                    .addComponent(jLabel8))
                .addGap(18, 18, 18)
                .addComponent(jLabel10)
                .addGap(18, 18, 18)
                .addComponent(jlblStatus, javax.swing.GroupLayout.DEFAULT_SIZE, 25, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbtnReset, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jbtnHintConfig, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jbtnSaveChanged, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(19, 19, 19))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 569, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jbtnResetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnResetActionPerformed
        resetAllField();
    }//GEN-LAST:event_jbtnResetActionPerformed

    private void jbtnSaveChangedActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnSaveChangedActionPerformed
        String svName = jtxtServerName.getText().trim();
        String dbName = jtxtDatabaseName.getText().trim();
        String usName = jtxtUserName.getText().trim();
        String pass = String.valueOf(jpsfPassword.getPassword()).trim();
        jlblStatus.setText("");

        DatabaseManagement dmng = new DatabaseManagement();
        Database db = new Database();
        db.setDatabaseName(dbName);
        db.setServerName(svName);
        db.setUserName(usName);
        db.setPassword(pass);
        if (!Validator.checkNull(svName)) {
            jtxtServerName.setFocusable(true);
            jlblStatus.setText("Lets enter server name");
            jlblStatus.setForeground(Color.red);
        } else if (!Validator.checkNull(usName)) {
            jtxtServerName.setFocusable(true);
            jlblStatus.setText("Lets enter user name");
            jlblStatus.setForeground(Color.red);
        } else if (!Validator.checkNull(pass)) {
            jpsfPassword.setFocusable(true);
            jlblStatus.setText("Lets enter password");
            jlblStatus.setForeground(Color.red);
        } else if (!Validator.checkNull(dbName)) {
            jtxtDatabaseName.setFocusable(true);
            jlblStatus.setText("Lets enter database name");
            jlblStatus.setForeground(Color.red);
        } else {
            if (dmng.configDatabase(db)) {
                jlblStatus.setText("Success!!! Database has been configuration!");
                jlblStatus.setForeground(Color.BLUE);
            } else {
                jlblStatus.setText("Failure!!! Database configuration error!");
                jlblStatus.setForeground(Color.RED);
            }
        }

    }//GEN-LAST:event_jbtnSaveChangedActionPerformed

    private void jbtnHintConfigActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnHintConfigActionPerformed
        jlblStatus.setText("");
        DatabaseManagement dmng = new DatabaseManagement();
        Database db = new Database();
        db = dmng.getDatabaseParam();

        jtxtDatabaseName.setText(db.getDatabaseName());
        jtxtServerName.setText(db.getServerName());
        jtxtUserName.setText(db.getUserName());
        jpsfPassword.setText(db.getPassword());
    }//GEN-LAST:event_jbtnHintConfigActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JButton jbtnHintConfig;
    private javax.swing.JButton jbtnReset;
    private javax.swing.JButton jbtnSaveChanged;
    private javax.swing.JLabel jlblStatus;
    private javax.swing.JPasswordField jpsfPassword;
    private javax.swing.JTextField jtxtDatabaseName;
    private javax.swing.JTextField jtxtServerName;
    private javax.swing.JTextField jtxtUserName;
    // End of variables declaration//GEN-END:variables
}
